<?php

namespace app\model;


use think\Model;

class Users extends Model
{
    protected $table = "users";
    protected $pk = "user_id";

    public function signIn($data){
        $res = self::create($data);
        return $res;
    }
    public function signOn($data){
        $res = self::get([
            'user_id' => $data['user_id'],
            'user_pwd' => $data['user_pwd']
        ]);
        return $res;
    }

    public static function check($user_id){
        return self::where("user_id",'=',$user_id)->count();
    }

    public function edit($id,$data){
        $res = $this->allowField(true)->save($data,['user_id'=>$id]);
        return $res;
    }

    public function del($id){
        return self::get($id)->delete();
    }

    public function getUserById($id){
        $res = self::get(['user_id' => $id]);
        if($res){
            $res = $res->hidden(['user_pwd']);
        }
        return $res;
    }
    public function searchUsers($key){
        return self::whereOr([
            'user_id' => ['like',"%" . $key . "%"],
            'user_name' => ['like',"%" . $key . "%"],
            'user_phone' => ['like',"%" . $key . "%"],
            'user_email' => ['like',"%" . $key . "%"]
        ])->select();
    }
    public function friendGroup(){
        return $this->hasMany('FriendGroup','user_id','user_id');
    }

    public function friends(){
        return $this->hasMany("Friends",'user_id','user_id');
    }
    public function friendchat(){
        return $this->belongsToMany("Chat",'friends','user_id','user_id');
    }
    public function getStatusAttr($value)
    {
        $status = [-1=>'离职',0=>'冻结',1=>'正常',2=>'审核'];
        return $status[$value];
    }
    public function getAllCount(){
        return self::count();
    }
    public function getAllUserByPage($page,$limit){
        return self::join('departments','users.department_id = departments.department_id')
            ->join('map_area','users.address_id = map_area.area_id')
            ->join('map_city','map_city.city_id = map_area.city_id')
            ->join('map_province','map_area.province_id = map_province.province_id')
            ->join('role','role.role_id = users.role_id')
            ->field([
            'role.role_id,role.role_name,map_area.province_id,map_area.city_id,map_area.area_id,area_name,city_name,province_name,users.*,users.user_birthday as user_age,users.user_birthday as user_animals,users.user_birthday as user_constellation,departments.department_name'
        ])->page($page,$limit)->select();
    }
    public function getDetail($user_id){
        return self::join('departments','users.department_id = departments.department_id')
            ->join('map_area','users.address_id = map_area.area_id')
            ->join('map_city','map_city.city_id = map_area.city_id')
            ->join('map_province','map_area.province_id = map_province.province_id')
            ->join('role','role.role_id = users.role_id')
            ->join('user_real','user_real.user_id = users.user_id')
            ->field([
                'user_real.*,role.role_id,role.role_name,map_area.province_id,map_area.city_id,map_area.area_id,area_name,city_name,province_name,users.*,users.user_birthday as user_age,users.user_birthday as user_animals,users.user_birthday as user_constellation,departments.department_name'
            ])->where(['users.user_id' => $user_id])->find();
    }
    public function getAllName(){
        return self::field(['user_id,user_name'])->select();
    }
    public function getTaskTarget(){
        return self::join('user_real','users.user_id = user_real.user_id')
            ->join('role','users.role_id = role.role_id')
            ->join('departments','users.department_id = departments.department_id')
            ->field(['users.user_id as id,concat(departments.department_name,"-",role.role_name,"-",user_real.real_name) as text,user_real.real_name as name'])->select();
    }
    public function getMyDepartmentTaskTarget($department_id){
        return self::join('user_real','users.user_id = user_real.user_id')
            ->join('role','users.role_id = role.role_id')
            ->join('departments','users.department_id = departments.department_id')
            ->where(['departments.department_id' => $department_id])
            ->field(['users.user_id as id,concat(departments.department_name,"-",role.role_name,"-",user_real.real_name) as text,user_real.real_name as name'])
            ->select();
    }
    public function getUserAgeAttr($value){
        if(!is_int($value) || $value < 1000) return $value;
        $birthday=date('Y-m-d',$value);
        list($year,$month,$day)=explode('-',$birthday);
        $cm=date('n');
        $cd=date('j');
        $age=date('Y')-$year-1;
        if ($cm>$month || $cm==$month && $cd>$day) $age++;
        return $age;
    }
    public function getUserAnimalsAttr($value){
        //格式化时间戳
        if(!is_int($value)) return $value;
        $birthday=date('Y-m-d',$value);
        //切割字符串
        list($year,$month,$day)=explode('-',$birthday);
        $animals = ['鼠', '牛', '虎', '兔', '龙', '蛇','马', '羊', '猴', '鸡', '狗', '猪'];
        //生肖算法为：（年-1900）求模12
        $key = ($year - 1900) % 12;
        return $animals[$key];
    }
    public function getUserConstellationAttr($value){
        if(!is_int($value)) return $value;
        $birth=date('Y-m-d',$value);
        list($year,$month,$day)=explode('-',$birth);
        $constellation_name = ['水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座','狮子座','处女座','天秤座','天蝎座','射手座','摩羯座'];
        $day <= 22 ? ('01' !== $month ? $constellation = $constellation_name[$month-2] : $constellation = $constellation_name[11]) : $constellation = $constellation_name[$month-1];
        return $constellation;
    }

    public function checkHas($user_id){
        return self::get($user_id);
    }
    public function checkDepartment($department_id){
        return self::where(['department_id' => $department_id])->count();
    }
    public function checkRole($role_id){
        return self::where(['role_id' => $role_id])->count();
    }
    //registe_time
    public function getRegisteTimeAttr($value){
        return date('Y-m-d H:i:s', $value);
    }
    public function getUserBirthdayAttr($value){
        return date('Y-m-d',$value);
    }
    public function getGraduationTimeAttr($value){
        return date('Y-m',$value);
    }
}